package com.example.dps;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ConfirmTransferActivity extends Activity{
	
	
	TransferDBHelper transferDBHelper;
	AccountDBHelper accountDBHelper;
	HistoryHelper historyHelper;
	DecimalFormat formatter = new DecimalFormat("$#,##0");
	
	DBHelper dbHelper;
	WebHelper webHelper;
	
	boolean firstTransfer = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.confirm_transfer_layout);
		
		transferDBHelper = new TransferDBHelper(this);
		accountDBHelper = new AccountDBHelper(this);
		this.dbHelper = new DBHelper(this);
		this.historyHelper = new HistoryHelper(this);
		this.webHelper = new WebHelper(this);
		
		/** grab all the info from the previous intent **/
		
		Bundle extras = this.getIntent().getExtras();
		
		int amount = extras.getInt("amount");
		String from = extras.getString("from");
		String to = extras.getString("to");
		String frequency = extras.getString("frequency");
		String starting = extras.getString("starting");
		String ending = extras.getString("ending");
		
		
		TextView amountText = (TextView)findViewById(R.id.confirmAmountext);
		TextView fromText = (TextView)findViewById(R.id.confirmFromText);
		TextView toText = (TextView) findViewById(R.id.confirmToText);
		TextView frequencyText = (TextView) findViewById(R.id.confirmFrequencyText);
		TextView startingText = (TextView) findViewById(R.id.confirmStartingText);
		TextView endingText = (TextView) findViewById(R.id.confirmEndingText);
		
		amountText.setText(formatter.format(amount));
		fromText.setText(from);
		toText.setText(to);
		frequencyText.setText(frequency);
		startingText.setText(starting);
		endingText.setText(ending);
		
		
		
		addOnClickListeners();
		
	}
	
	public void addOnClickListeners(){
		
		ImageButton continueButton = (ImageButton) findViewById(R.id.confirmTransferSubmitButton);
		
		continueButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View view) {
				
				/** complete the transfer **/
				
				handleTransfer();
				
			}
			
		});
		
		continueButton.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	ImageView view = (ImageView) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.submit_transfer_btn_clicked));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.submit_transfer_btn));
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.submit_transfer_btn));
		        }
				return false;
			}
		});
		
		ImageButton editButton = (ImageButton) findViewById(R.id.confirmTransferEditButton);
		editButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				ConfirmTransferActivity.this.finish();
				
			}
				
		});
		
		
	}
	
	
	public void handleTransfer(){
		

		
		
		TextView amountText = (TextView)findViewById(R.id.confirmAmountext);
		TextView fromText = (TextView)findViewById(R.id.confirmFromText);
		TextView toText = (TextView) findViewById(R.id.confirmToText);
		TextView frequencyText = (TextView) findViewById(R.id.confirmFrequencyText);
		TextView startingText = (TextView) findViewById(R.id.confirmStartingText);
		TextView endingText = (TextView) findViewById(R.id.confirmEndingText);
		
		int amount = Integer.parseInt(amountText.getText().toString().replace("$", "").replace(",", ""));
		String from = fromText.getText().toString();
		String to = toText.getText().toString();
		String frequency = frequencyText.getText().toString();
		String starting = startingText.getText().toString();
		String ending = endingText.getText().toString();
		
		boolean madeDeposit = this.dbHelper.getBoolean(dbHelper.COLUMN_MADE_DEPOSIT);
		
		this.webHelper.getAccountInfo();
		
		
		/** if user has NOT made a deposit, this is the first transfer **/
		if(!madeDeposit) firstTransfer = true;
		else firstTransfer = false;

		if(frequency.equals("Once, Now")){
			
			if(webHelper.performTransfer(from, to, amount) == false){
				Toast.makeText(this, "Not enough money in account!", Toast.LENGTH_LONG).show();
				
				return;
			}
			
//			if(accountDBHelper.transferMoney(from, to, amount) == false){
//				Toast.makeText(this, "Not enough money in account!", Toast.LENGTH_LONG).show();
//				
//				return;
//			}
			else{
				if(firstTransfer == false){
					
					this.webHelper.updateAccountInfo();
					
					/**update transfer date **/
					this.dbHelper.setLastDeposit();
					Log.e("****", "***************");
					this.historyHelper.addTransfer(from, to, amount);
					
					
					/** opens tabbed view starting on view savings **/
					Intent intent = new Intent(ConfirmTransferActivity.this, TabRoot.class);
					intent.putExtra("START_TAB", "view_savings");
					ConfirmTransferActivity.this.startActivity(intent);
				}
				
			}

		}
		else{
			Log.e("adsf", "check point 3");
			
			/** if frequency once, schedule, the ending is the same as the starting **/
			if(frequency.equals(getResources().getStringArray(R.array.transfer_frequency_array)[1])){
				
				ending = starting;
				
			}
			
			Log.e("CONFIRM TRANSFER", "CHECK POINT ___ before addtranfer");
			//add to recurring transfer
			transferDBHelper.addTransfer(amount, from, to, frequency, starting, ending);
			
			if(firstTransfer == false){
				Intent intent = new Intent(ConfirmTransferActivity.this, TabRoot.class);
				//Log.d("Main Activity", "Starting Mortgage Calc Activity");
				intent.putExtra("START_TAB", "add_money");
				ConfirmTransferActivity.this.startActivity(intent);
			}

		}
		
		//it was the first transfer,
		//change to the first transfer layout
		if(firstTransfer == true){
			
			firstTransfer=false;
			this.dbHelper.setBoolean(dbHelper.COLUMN_FIRST_TIME, 0);
			
			this.historyHelper.addTransfer(from, to, amount);
			
			this.webHelper.updateAccountInfo();
			
			setContentView(R.layout.first_transfer_layout);
			
			ImageButton continueButton = (ImageButton) findViewById(R.id.firstTransferContinueButton);
			
			continueButton.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					
					ConfirmTransferActivity.this.webHelper.updateAccountInfo();
					ConfirmTransferActivity.this.dbHelper.setBoolean(dbHelper.COLUMN_MADE_DEPOSIT, 1);
					Intent intent = new Intent(ConfirmTransferActivity.this, TabRoot.class);
					intent.putExtra("START_TAB", "view_savings");
					ConfirmTransferActivity.this.startActivity(intent);
					
				}
				
			});

			continueButton.setOnTouchListener(new OnTouchListener(){
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					/** get the root element of the button view **/
			    	ImageView view = (ImageView) v;
			    	
			        if(event.getAction() == MotionEvent.ACTION_DOWN) {
			        	/** change the image button picture when clicked **/
			        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.view_savings_btn_clicked));
			        } else if (event.getAction() == MotionEvent.ACTION_UP) {
			        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.view_savings_btn));
			        }
			        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
			        	view.setImageDrawable(v.getResources().getDrawable(R.drawable.view_savings_btn));
			        }
					return false;
				}
			});
			
		}
		
	}

}
